package workforce.limousine.struts.core;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import limousine.dbservice.ImageService;
import limousine.util.Config;
import limousine.util.Dir;
import limousine.util.Func;
import limousine.util.Service;
import limousine.util.Token;
import limousine.vo.Image;
import limousine.vo.Users;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;



public class UploadImageDelete extends Action {
    private Func func = new Func();
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        System.out.println("***UploadImageDelete");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
       /* if (!user.getRole_name().equals("Admin")) {
            System.out.println("--Not Admin Hack--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        // delete
        String id = request.getParameter("did");

//        String catId = request.getParameter("catId");
        String catLock = request.getParameter("catLock");
        
        if (id != null) {// delete before
            
            ImageService cs = new ImageService();
            cs.setDataSource(ds);

            Image o = cs.getById(id);
            if(o != null){
                if(cs.deleteById(id)){
                    Dir dir = new Dir();
                    String target = Config.DIR_CONTENT_ROOT + dir.getFileSeperator() + Config.DIR_IMAGE + dir.getFileSeperator();
                    dir.deleteFile(target+o.getName());

                    ActionForward af = new ActionForward("uploadimageinit.do?catId="+o.getCategory()+"&catLock="+catLock, true);
                    return af;
                }
            }
        }
        session.setAttribute(Token.MSG_USER,Token.MSG_DELETE_SUCCESS);
        return mapping.findForward(Token.SUCCESS);
    }
}
